// JavaScript Document
var ZIndex = 555;

//阻止冒泡事件
function cancelBubble(e)
{
  if (e.stopPropagation) e.stopPropagation();
  else e.cancelBubble = true;
}

//弹出层

function setDialog(obj,f){
   	var dialogname = "dialogbox";
	var width_inner;
    if(f == "first"){ dialogname = "dialogboxfirst";}
	if(f == "fullwidth"){ width_inner = true;}
	if(arguments[2] == "mask"){ setMask();}
    if($("#"+dialogname)[0]){
        var con = $("#"+obj);
        var dialogbox = $("#"+dialogname);
        $(document.body).append(dialogbox.find("td.box_inner").children().hide());
        dialogbox.show().find("td.box_inner").append(con.show());
		if(width_inner) con.width(document.body.clientWidth-40);
    }else{
        var dialogbox = document.createElement("div");
        dialogbox.className = "dialog"; dialogbox.id = dialogname;
        var jinnerStr = $('<table style="width:auto" class="box_wrapper" cellpadding="0" cellspacing="0" border="0"> <tr> <td class="l_t"></td> <td class="t_line"></td> <td class="r_t"></td> </tr> <tr> <td class="l_line"></td> <td class="box_inner"></td> <td class="r_line"></td> </tr> <tr> <td class="l_b"></td> <td class="b_line"></td> <td class="r_b"></td> </tr> </table><span class="x_btn"></span>');
        //dialogbox.innerHTML = '<div class="dialog_inner"></div><span class="x_btn"></span>';
        var content = $("#"+obj);
        $(dialogbox).append(jinnerStr);
        jinnerStr.find(".box_inner").append(content.show());
        $(document.body).append(dialogbox);
		if(width_inner) content.width(document.body.clientWidth-40);

        $(dialogbox).find(".x_btn").bind("click",function(){
            $(this).parent().hide();
            $(this).parent().find("span.float_tip").remove();
            $("#tree").hide();
			closeMask();
        }).mousedown(function(){
            $(this).addClass("x_btn_down");
        }).mouseup(function() {
            $(this).removeClass("x_btn_down");
        }).hover(function() {
            $(this).addClass("x_btn_hover");
        },function() {
            $(this).removeClass("x_btn_hover x_btn_down");
        });
		var doc = window,_$doc = $(doc.document.documentElement);
		var _fn = function(){};
		var dragEvent = {
			onstart: _fn,
			start: function(event)
			{
				var that = dragEvent;
				
				_$doc
				.bind( 'mousemove', that.move )
				.bind( 'mouseup', that.end );
				
				that._sClientX = event.clientX;
				that._sClientY = event.clientY;
				that.onstart( event.clientX, event.clientY );
				
				return false;
			},
			
			onmove: _fn,
			move: function(event)
			{
				var that = dragEvent;
				
				that.onmove(
					event.clientX - that._sClientX,
					event.clientY - that._sClientY
				);
				
				return false;
			},
			
			onend: _fn,
			end: function(event)
			{
				var that = dragEvent;
				
				_$doc
				.unbind('mousemove', that.move)
				.unbind('mouseup', that.end);
				
				that.onend(  event.clientX, event.clientY );
				return false;
			}	
		}
		_use = function(event)
		{
			var startWidth, startHeight, startLeft, startTop,
				wrap = dialogbox,
				_dragEvent = dragEvent,
				_doc = doc.document.documentElement,
			
			// 清除文本选择
			clsSelect = 'getSelection' in _doc ?
			function(){
				_doc.getSelection().removeAllRanges();
			}:function(){
				try{_doc.selection.empty();}catch(e){};
			};
			
			// 对话框准备拖动
			_dragEvent.onstart = function( x, y )
			{
				startLeft = wrap.offsetLeft;
				startTop = wrap.offsetTop;
				
				_$doc.bind( 'dblclick', _dragEvent.end );
			};
			
			// 对话框拖动进行中
			_dragEvent.onmove = function( x, y )
			{
				var style = wrap.style,
					left = x + startLeft,
					top = y + startTop;
	
				style.left = Math.max( limit.minX, Math.min(limit.maxX,left) ) + 'px';
				style.top = Math.max( limit.minY, Math.min(limit.maxY,top) ) + 'px';
				$(wrap).find("strong.float_tip").each(function(){
					var $this = $(this);
					var txt = $this.siblings("textarea")[0] ? $this.siblings("textarea") :	$this.siblings("input[type=text]");
					$this.css({"top":txt.offset().top+'px',"left":txt.offset().left+'px'});
				});
				clsSelect();
			};
			
			// 对话框拖动结束
			_dragEvent.onend = function( x, y )
			{
				_$doc.unbind('dblclick',_dragEvent.end);
				
			};
			limit =	(function(fixed)
			{
				var	ow = wrap.offsetWidth,
					// 向下拖动时不能将标题栏拖出可视区域
					oh = wrap.getElementsByTagName("h2").offsetHeight || 20,
					ww = _$doc.find('body').width(),
					wh = 1100,
					dl = fixed ? 0 : _$doc.find('body').scrollLeft(),
					dt = fixed ? 0 : _$doc.find('body').scrollTop();
					// 坐标最大值限制(在可视区域内)	
					maxX = ww - ow + dl;
					maxY = wh - oh + dt;
				
				return {
					minX: dl,
					minY: dt,
					maxX: maxX,
					maxY: maxY
				};
			})(wrap.style.position === 'fixed');
			
			_dragEvent.start(event);
		};
		$(dialogbox).find("h2").bind("mousedown",function(event){
			_use(event);
		})
    }
    boxCenter(dialogbox);
	$(dialogbox).find("strong.float_tip").each(function(){
		var $this = $(this);
		var txt = $this.siblings("textarea")[0] ? $this.siblings("textarea") :	$this.siblings("input[type=text]");
		$this.css({"top":txt.offset().top+'px',"left":txt.offset().left+'px'});
	});
}


//关闭弹出层
function closeDialog(txt){
    $("#dialogbox").hide().find("span.float_tip").remove();
	if(txt == "nofresh"){}
	else{
    	myrefresh();
	}
	closeMask();
}
function closeFirstDialog(txt){
    $("#dialogboxfirst").hide().find("span.float_tip").remove();
   if(txt == "nofresh"){}
	else{
    	myrefresh();
	}
	closeMask();
}
//设置遮罩层
function setMask(){
	var maskdiv;
	if($("#mask_div")[0]){
		maskdiv = document.getElementById("mask_div");
	}else{
		maskdiv = document.createElement("div");	
		maskdiv.className = "mask_div";
		maskdiv.id = "mask_div";
		document.body.appendChild(maskdiv);
	}
	ZIndex += 1;
	maskdiv.style.zIndex = ZIndex;
	maskdiv.style.display = "block";
}
//关闭遮罩层
function closeMask(){
	if($("#mask_div")[0]){
		document.getElementById("mask_div").style.display = "none";	
	}
}
//整体居中
function boxCenter(obj){
    var doc = window.parent? window.parent: window,content = $(obj),dom = doc.document.documentElement;
    var left,top;
    if(window.parent){
        left = (parseInt(dom.offsetWidth)-content.width())/2 -140;
        top = (parseInt( doc.screen.height)-content.height())/2 -145 + dom.scrollTop+doc.document.body.scrollTop;
    }else{
        left = (parseInt(dom.offsetWidth)-content.width())/2;
        top = (parseInt( doc.screen.height)-content.height())/2 +  dom.scrollTop+doc.document.body.scrollTop;
    }
	//alert(content.find(".table2").find("tr").length)
	if((top + content.height()+110) > 1000){
		top = 1000 - content.height();	
	}
    ZIndex += 1;
    content.css({"top":(top <0 ? 0 : top)+"px","left":(left < 0 ? 0 : left)+"px","z-index":ZIndex});

}


